package com.yhy.common.wechat.config;

import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.api.impl.WxMaServiceImpl;
import cn.binarywang.wx.miniapp.config.impl.WxMaDefaultConfigImpl;
import cn.hutool.core.collection.CollUtil;
import com.github.binarywang.wxpay.config.WxPayConfig;
import com.github.binarywang.wxpay.service.WxPayService;
import com.github.binarywang.wxpay.service.impl.WxPayServiceImpl;
import com.google.common.collect.Maps;
import com.yhy.common.wechat.properties.WxMaProperties;
import com.yhy.common.wechat.properties.WxMpProperties;
import com.yhy.common.wechat.properties.WxPayProperties;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl;
import me.chanjar.weixin.mp.config.impl.WxMpDefaultConfigImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Slf4j
@ComponentScan("com.yhy.common.wechat")
@RequiredArgsConstructor
public class WxConfig {

    private static Map<String, WxMpService> mpService = Maps.newHashMap();
    private static Map<String, WxMaService> maService = Maps.newHashMap();
    private static Map<String, WxPayService> payService = Maps.newHashMap();

    private final WxMpProperties mpProperties;
    private final WxMaProperties maProperties;
    private final WxPayProperties payProperties;

    public static Map<String, WxMpService> getMpServices() {
        return mpService;
    }

    public static Map<String, WxMaService> getMaServices() {
        return maService;
    }

    public static Map<String, WxPayService> getPayServices() {
        return payService;
    }

    @Bean
    public Map<String, WxMpService> mpService() {
        final List<WxMpProperties.MpConfig> configs = this.mpProperties.getConfigs();
        if (CollUtil.isEmpty(configs) || configs.parallelStream().anyMatch(config ->
                StringUtils.isAnyBlank(config.getAppId(), config.getSecret(), config.getToken(), config.getAesKey()))) {
            log.warn("未配置必要的公众号参数");
            return null;
        }

        mpService = configs.stream().map(a -> {
            WxMpDefaultConfigImpl configStorage = new WxMpDefaultConfigImpl();
            configStorage.setAppId(a.getAppId());
            configStorage.setSecret(a.getSecret());
            configStorage.setToken(a.getToken());
            configStorage.setAesKey(a.getAesKey());

            WxMpService service = new WxMpServiceImpl();
            service.setWxMpConfigStorage(configStorage);
            return service;
        }).collect(Collectors.toMap(service -> service.getWxMpConfigStorage().getAppId(),
                config -> config, (key, val) -> key));
        return mpService;
    }

    @Bean
    public Map<String, WxMaService> maService() {
        final List<WxMaProperties.MaConfig> configs = this.maProperties.getConfigs();
        if (CollUtil.isEmpty(configs) || configs.parallelStream()
                .anyMatch(config -> StringUtils.isAnyBlank(config.getAppId(), config.getSecret()))) {
            log.warn("未配置必要的小程序参数");
            return null;
        }

        maService = configs.stream().map(a -> {
            WxMaDefaultConfigImpl configStorage = new WxMaDefaultConfigImpl();
            configStorage.setAppid(a.getAppId());
            configStorage.setSecret(a.getSecret());
            WxMaService service = new WxMaServiceImpl();
            service.setWxMaConfig(configStorage);
            return service;
        }).collect(Collectors.toMap(service -> service.getWxMaConfig().getAppid(),
                config -> config, (key, val) -> key));
        return maService;
    }

    @Bean
    public Map<String, WxPayService> payService() {
        final List<WxPayProperties.PayConfig> configs = this.payProperties.getConfigs();
        if (CollUtil.isEmpty(configs) || configs.parallelStream().anyMatch(config -> StringUtils.isAnyBlank(
                config.getAppId(), config.getSecret(), config.getMchId(), config.getMchKey(), config.getNotifyUrl()))) {
            log.warn("未配置必要的微信支付参数");
            return null;
        }

        payService = configs.stream().map(config -> {
            WxPayConfig configStorage = new WxPayConfig();
            configStorage.setAppId(config.getAppId());
            configStorage.setMchId(config.getMchId());
            configStorage.setMchKey(config.getMchKey());
            configStorage.setNotifyUrl(config.getNotifyUrl());
            WxPayService service = new WxPayServiceImpl();
            service.setConfig(configStorage);
            return service;
        }).collect(Collectors.toMap(service -> service.getConfig().getAppId(),
                config -> config, (key, val) -> key));
        return payService;
    }
}
